package org.display.graphic
{

	import flash.events.*;
	import flash.filesystem.File;
	import flash.ui.*;
	import flash.utils.*;
	
	import mx.controls.*;
	import mx.events.*;
	import mx.managers.*;
	
	import org.data.*;
	import org.display.graphic.*;
	import org.display.util.PublicJsfl;
	import org.net.*;
	import org.utils.*;
	
	public class IvContainer extends Iv
	{
		var tab
		var scene
		var sceneTab
		
		var classname:String=""
			
		public function IvContainer(xmlpath:String,tab,scene,sceneTab)
		{
			this.tab=tab
			this.scene=scene
			this.sceneTab=sceneTab
				
			super(xmlpath)
			
		
		}
		
		function refreshMenu(){
			
			var menus:Array=["编辑","添加数据","添加异步事件","添加功能事件","发布"]
				
			modifyMenus(classname+"View")
			
			modifyMenus(classname+"Model")
			
			modifyMenus(classname+"ViewProxy")
			
			modifyMenus(classname+"ModelProxy")
			
			createMenuItems(menus)
				
			function modifyMenus(className:String){
				
				var objpath:String= getopenPath(className)	
				
				var file:File = File.desktopDirectory.resolvePath(objpath);	
				
				if(file.exists){menus.push(className)}
				
			}
		
		
		}
		
		protected override function loadView(){
			
			
			
			var xmlRes:AbcResponder=new AbcResponder()
				
			xmlRes.onResult=function(e,path){
				
				viewdata=e
					
			    classname=getViewName().replace("View","")
				
				//createMenuItems(["编辑","添加数据","添加异步事件","添加功能事件","发布","测试",classname+"View",classname+"Model",classname+"ViewProxy",classname+"ModelProxy"])
				
				refreshMenu()
				
			}
				
			var path:String=(Global.workspace+"/"+String(data.view)).replace(/\./gis,"/").replace(/\\/gis,"\/")+".as"
			
			new AbcHTTPServices(path,xmlRes)		
		}
		
		
		
		
		protected override function loadViewProxy(){
			
			var xmlRes:AbcResponder=new AbcResponder()
			
			xmlRes.onResult=function(e,path){
				
				addEventSquare(e)
				
			}
			
			var path:String=(Global.workspace+"/"+String(data.view)).replace(/\./gis,"/").replace(/\\/gis,"\/").replace(/\/view\//gis,"/viewProxy/")+"Proxy.as"
				
			new AbcHTTPServices(path,xmlRes)		
		}
		
		function addEventSquare(value:String){
		
			var events:Array=value.match(/(?<=AbcEventDispatcher\.instance\.dispatcher\(").*?AsynEvent/gis)
				
			events=events.concat(value.match(/(?<=AbcEventDispatcher\.instance\.dispatcher\(").*?FunEvent/gis))
			
			for(var i=0;i<events.length;i++){
			
			    var str:String=events[i].replace(/\s/gis,"")
				
				if(str.indexOf("AsynEvent")!=-1){
					
				var square:EventSquare=new EventSquare(str,0xFFFFCC,0xFCF27F)
					
				}else if(str.indexOf("FunEvent")!=-1){
				
				 square=new EventSquare(str,0xFFFFCC,0xCCFF00)
				
				}
				
				this.addChild(square)
				
				square.x=-square.width
					
				square.y=square.height*i
			
			}

		}
		
		public function check(obj):Boolean{
		
		
		   var ivs:Array=ElementControl.getObjs(Iv,this)
			   
		   for(var i=0;i<ivs.length;i++){
			   
			 // trace("file:///"+Iv(ivs[i]).path.replace(/\\/gis,"/"),obj.path)

			   var ivpath:String=Iv(ivs[i]).path.replace(/\\/gis,"/").replace(/.*?(?=\/xml\/)/gis,"")
				   
			   var objPath:String=obj.path.replace(/.*?(?=\/xml\/)/gis,"")
		   
		       if(ivpath==objPath){
			   
			      return true
				  
			   }

		   }
		   
		   return false 
		
		
		}
		
		
		function getopenPath(type:String):String{
		
			var eventName:String=this.getViewName()
			
			var openpath:String=path.replace("/xml/","/")
			
			var arr:Array=openpath.split("/")
			arr.pop()
			arr.push(StringControl.onewordLower(type.replace(classname,"")))
			
			if(type==classname+"View"){
				
				arr.push(eventName+".as")	
				
			}else if(type==classname+"ViewProxy"){
				
				arr.push(eventName+"Proxy.as")
			}
			if(type==classname+"ModelProxy"){
				
				arr.push(eventName.replace("View","Model")+"Proxy.as")
				
			}else if(type==classname+"Model"){
				
				arr.push(eventName.replace("View","Model")+".as")
				
			}
				
			return arr.join("/")
			
		}
		
		
		protected override  function menuselectEvent(e:ContextMenuEvent){
			
			var type:String=ContextMenuItem(e.currentTarget).caption

			switch(type){

				case classname+"View":
					
				case classname+"Model":
					
				case classname+"ViewProxy":
		
				case classname+"ModelProxy":
					
					try{
						
					var openpath=getopenPath(type)
					trace(openpath)
					var file:File = File.desktopDirectory.resolvePath(openpath);
					file.openWithDefaultApplication();
					
					}catch(e:Error){
					
					Alert.show(openpath+"不存在！")
					
					}
					
					break
				
				case "编辑":
					
					if(Global.editObj!=scene){

						if(path==Iv(Global.editObj).path){
							Alert.show("不能把自己添加到自己内部")
							return;}
						
					}

					tab.selectedIndex=0
					
					var iv:Iv=new Iv(path)
					
					setTimeout(function(){
						if(Global.editObj==scene){
						   Global.editObj.addChild(iv)
						}else{
						   Global.editObj.addChild(iv)
						   iv.createMenuItems(["重命名","复制","最上层","最下层","删除"])
						}
					
					},200)
						
					break
				/*
				case "编辑":
					
					tab.selectedIndex=0
					
					ElementControl.cleanObjByType(ShapeBase,scene)
					
					var iv:Iv=new Iv(path)
					
					setTimeout(iv.setObjs,100,scene)
					
					Global.xmlPath=path

					sceneTab.label="Scene>>"+Global.xmlPath.split("/")[Global.xmlPath.split("/").length-1]
					
					break
				*/
				case "删除":
					
					ElementControl.cleanObj(this)

					break
				
				
				case "添加数据":
					
					var modelPath:String=getopenPath(classname+"Model")
					
					if(File.desktopDirectory.resolvePath(modelPath).exists){
						addData(type)
						return;}
					
					Alert.yesLabel = "Yes";
					Alert.noLabel = "No";
					Alert.show(classname+"Model.as和"+classname+"ModelProxy.as文件不存在，是否生成？", type, 3, this, alertClickHandler);
				
					break
				
				case "添加异步事件":
					
				case "添加功能事件":
					
					var addFolderPannel=ETitlePannel(PopUpManager.createPopUp( this, ETitlePannel , true));
					addFolderPannel.title=ContextMenuItem(e.currentTarget).caption
					addFolderPannel.userName.text=""
					
					
					//file:///C:/abcTime/xml/org/display/component/album/AlbumView.xml
					//C:\abcTime\org\display\main\model\LineModel.as
					var arr:Array = path.replace("file:///","").replace("/xml/","/").replace("View.xml","ViewProxy.as").split("/")
					arr.splice(arr.length-1,0,"viewProxy")
					Global.selectPath=arr.join("\\")
					
					
					break
				
				case "发布":
					
					//file:///C:/abcTime/xml/org/display/component/album/AlbumView.xml

					var jsflPath:String = path.replace(/\/xml\//gis,"\/jsfl\/").replace(".xml",".jsfl")
					
					
					//trace(jsflPath.replace(/\/jsfl\//gis,"\/xml\/").replace(".jsfl",".xml"))
					
					//var file:File = File.desktopDirectory.resolvePath(jsflPath);
					//file.openWithDefaultApplication();
					
					new PublicJsfl(jsflPath.replace(/\/jsfl\//gis,"\/xml\/").replace(".jsfl",".xml"))
					
					break
				
				
				case "测试":
					
					//新建模块fla文件
					var sourceFile:File = File.documentsDirectory;
					var path:String=Global.workspace.replace(/\\/gis,"/")+"/org/abcFrame/templete/main.fla"
					sourceFile = sourceFile.resolvePath(path);
					var destination:File = File.documentsDirectory;
					
					destination = destination.resolvePath(Global.workspace.replace(/\\/gis,"/")+"/swf/test/"+this.getViewName()+".fla");
					sourceFile.copyTo(destination, true)
					
					//自动打开新建模块fla文件
					destination.openWithDefaultApplication();
					
					
					setTimeout(function(){
					
						var jsflPath:String=path.replace(/\/xml\//gis,"\/jsfl\/").replace(".xml",".jsfl")
						//var file:File = File.desktopDirectory.resolvePath(jsflPath);
						//file.openWithDefaultApplication();
						
						new PublicJsfl(jsflPath.replace(/\/jsfl\//gis,"\/xml\/").replace(".jsfl",".xml"))
					
					},2000)
					
					
					
					break
				
				
				
			}
			
			
			
		}
		
		function addData(title:String){
		
		
			var addFolderPannel:ETitlePannel=ETitlePannel(PopUpManager.createPopUp( this, ETitlePannel , true));
			addFolderPannel.title=title
			addFolderPannel.userName.text="value : String"
			
			
			//file:///C:/abcTime/xml/org/display/component/album/AlbumView.xml
			//C:\abcTime\org\display\main\model\LineModel.as
			var arr:Array = path.replace("file:///","").replace("/xml/","/").replace("View.xml","Model.as").split("/")
			arr.splice(arr.length-1,0,"model")
			Global.selectPath=arr.join("\\")
		
		
		}
		
		
		function alertClickHandler(event:CloseEvent):void {
			
			if (event.detail==Alert.NO){return;}
			
			domodel()
			
			doModelProxy()
			
			doViewProxy()
			
			refreshMenu()
			
			addData(Alert(event.target).title)
			
		}
		
		function domodel(){
			
			var modelPath:String=getopenPath(classname+"Model")

			var package_model:String= modelPath.replace(/.*?(?=\/org\/)/,"").replace(/\//,"").replace(/\.as/,"").replace(/\//gis,".")
				
			//trace(package_model)	
				
			var arr:Array=package_model.split(".")
				
			arr.pop()
				
			package_model=arr.join(".")
				
			var source:String=Global.modelSource.replace("<package>",package_model)
				
			source=source.replace(/Default/gis,classname)
			
			FileControl.saveFile(modelPath,source)
			
		}
		
		function doModelProxy(){
			
			var modelProxyPath:String=getopenPath(classname+"ModelProxy")

			var package_modelProxy=modelProxyPath.replace(/.*?(?=\/org\/)/,"").replace(/\//,"").replace(/\.as/,"").replace(/\//gis,".")
			
			var arr:Array=package_modelProxy.split(".")
			
			arr.pop()	
				
			package_modelProxy=arr.join(".")
				
			var import_modelProxy ="import "+package_modelProxy.replace("modelProxy","model")+".DefaultModel;\n       "
			     import_modelProxy+="import "+package_modelProxy.replace("modelProxy","view")+".DefaultView;\n       "
			
		    var source:String=Global.modelProxySource.replace("<package>",package_modelProxy)
				
			source=source.replace(/<import>/gis,import_modelProxy)
				 
			source=source.replace(/Default/gis,classname)
			
			FileControl.saveFile(modelProxyPath,source)
			
		}
		
		
		
		function doViewProxy(){
			
			var res:AbcResponder=new AbcResponder()
			
			res.onResult=function(e,path){
				
				var source:String=e
					
				var repstr:String="//init\n          view._modelProxy=new "+classname+"ModelProxy()\n          view._modelProxy.init(view)"

				source=source.replace("//init",repstr)
					
				var modelProxyPath:String=getopenPath(classname+"ModelProxy")

				var package_modelProxy=modelProxyPath.replace(/.*?(?=\/org\/)/,"").replace(/\//,"").replace(/\.as/,"").replace(/\//gis,".")
				
				var arr:Array=package_modelProxy.split(".")
				
				arr.pop()	
				
				package_modelProxy=arr.join(".")	
					
				var import_modelProxy="import "+package_modelProxy+"."+classname+"ModelProxy;\n       "
				import_modelProxy+="import "+package_modelProxy.replace("modelProxy","model")+"."+classname+"Model;\n         "
					
				repstr="//import\n	        "+import_modelProxy
				source=source.replace("//import",repstr)
					
				var funstr:String="public function get modelProxy():"+classname+"ModelProxy{return view._modelProxy as "+classname+"ModelProxy}\n      "
				repstr="//function\n	        "+funstr
				source=source.replace("//function",repstr)	
					
				setTimeout(FileControl.saveFile,100,path,source)
				
			}
			
			new AbcHTTPServices(getopenPath(classname+"ViewProxy"),res)
			
		}
		
		
		
		
		
		
		
	
	}
}